FROM paddlecloud/paddleocr:2.6-cpu-latest

# 禁用pip更新检查
ENV PIP_DISABLE_PIP_VERSION_CHECK=1
ENV PIP_NO_CACHE_DIR=1

# 安装必要的包
RUN apt-get update && apt-get install -y --no-install-recommends \
    fonts-noto-cjk \
    wget \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# 创建模型目录并下载模型
WORKDIR /workspace/models
# 下载中文检测模型（服务器端）
RUN if [ ! -d "ch_PP-OCRv4_det_infer" ]; then \
    wget https://paddleocr.bj.bcebos.com/PP-OCRv4/chinese/ch_PP-OCRv4_det_infer.tar && \
    tar xf ch_PP-OCRv4_det_infer.tar && \
    rm ch_PP-OCRv4_det_infer.tar; \
    fi && \
    # 下载中文识别模型（服务器端）
    if [ ! -d "ch_PP-OCRv4_rec_server_infer" ]; then \
    wget https://paddleocr.bj.bcebos.com/PP-OCRv4/chinese/ch_PP-OCRv4_rec_server_infer.tar && \
    tar xf ch_PP-OCRv4_rec_server_infer.tar && \
    rm ch_PP-OCRv4_rec_server_infer.tar; \
    fi && \
    # 下载英文检测模型
    if [ ! -d "en_PP-OCRv3_det_infer" ]; then \
    wget https://paddleocr.bj.bcebos.com/PP-OCRv3/english/en_PP-OCRv3_det_infer.tar && \
    tar xf en_PP-OCRv3_det_infer.tar && \
    rm en_PP-OCRv3_det_infer.tar; \
    fi && \
    # 下载英文识别模型
    if [ ! -d "en_PP-OCRv4_rec_infer" ]; then \
    wget https://paddleocr.bj.bcebos.com/PP-OCRv4/english/en_PP-OCRv4_rec_infer.tar && \
    tar xf en_PP-OCRv4_rec_infer.tar && \
    rm en_PP-OCRv4_rec_infer.tar; \
    fi && \
    # 下载文本方向分类模型
    if [ ! -d "ch_ppocr_mobile_v2.0_cls_infer" ]; then \
    wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar && \
    tar xf ch_ppocr_mobile_v2.0_cls_infer.tar && \
    rm ch_ppocr_mobile_v2.0_cls_infer.tar; \
    fi

# 创建字典目录并下载字典文件
RUN mkdir -p /workspace/dict && \
    wget -O /workspace/dict/ppocr_keys_v1.txt https://raw.githubusercontent.com/PaddlePaddle/PaddleOCR/release/2.7/ppocr/utils/ppocr_keys_v1.txt && \
    wget -O /workspace/dict/en_dict.txt https://raw.githubusercontent.com/PaddlePaddle/PaddleOCR/release/2.7/ppocr/utils/en_dict.txt

# 预安装所有依赖
RUN pip install --no-cache-dir --disable-pip-version-check paddleocr flask gunicorn

# 设置工作目录
WORKDIR /workspace

# 复制应用代码
COPY ocr_api_server.py /workspace/

# 默认命令
CMD ["gunicorn", "-b", "0.0.0.0:9000", "--workers=2", "--threads=4", "--timeout=120", "--preload", "ocr_api_server:app"] 